//
//  UIWebvc.m
//  testAAA
//
//  Created by tianNanYiHao on 2019/11/1.
//  Copyright © 2019 tianNanYiHao. All rights reserved.
//

#import "OldOC2JSvc.h"
#import "XWFT.h"

@interface OldOC2JSvc ()<UIWebViewDelegate>
@property (strong,nonatomic) UIWebView *web;
@property (nonatomic, strong) UIButton* btn1;
@property (nonatomic, strong) UIButton* btn2;
@property (nonatomic, assign) CGFloat halfHeight;
@property (nonatomic, strong ) UILabel *lab;
@end

@implementation OldOC2JSvc

//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e


-(UILabel*)lab{
    if (!_lab) {
        _lab = [[UILabel alloc] initWithFrame:CGRectMake(20, self.halfHeight+190, 280, 50)];
        _lab.text = @"等待js返回消息...";
        [_lab setBackgroundColor:[UIColor greenColor]];
    }
    return  _lab;
}

- (UIButton*)btn1{
    if (!_btn1) {
        _btn1 = [[UIButton alloc] init];
        [_btn1 setTitle:@"oc按钮, 点我注入js并执行" forState:UIControlStateNormal];
        _btn1.frame = CGRectMake(20, self.halfHeight+40, 280, 50);
        [_btn1 setBackgroundColor:[UIColor greenColor]];
        [_btn1 addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btn1;
}

- (UIButton*)btn2{
    if (!_btn2) {
        _btn2 = [[UIButton alloc] init];
        [_btn2 setTitle:@"oc按钮, 执行js代码" forState:UIControlStateNormal];
        _btn2.frame = CGRectMake(20, self.halfHeight+120, 280, 50);
        [_btn2 setBackgroundColor:[UIColor greenColor]];
        [_btn2 addTarget:self action:@selector(btnClick2) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btn2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor grayColor];
       self.halfHeight = [UIScreen mainScreen].bounds.size.height/2;
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 500)];
    self.web.delegate = self;
    
    
    // 加载本地模拟html
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Test3" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSString *basePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:basePath];
    [self.web loadHTMLString:htmlString baseURL:baseURL];
    [self.view addSubview:self.web];
    
    
    [self.view addSubview:self.btn1];
    [self.view addSubview:self.btn2];
    [self.view addSubview:self.lab];
    
}


- (void)btnClick{
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext） 相当于js环境中的 window
    JSContext *context = [self.web valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // 1.1 js如何注入?
    // 可以通过 context上下文 直接注入当前web的js代码, 在 html中, js直接执行getNum(10) 或 getNum(a) 方法  => js调用/oc提供实现方法
    [context evaluateScript:@"var a = 10"];
    [context evaluateScript:@"var getNum = function(param){return param*3}"];
    
    JSValue *va = [context evaluateScript:@"getNum(3)"];
    self.lab.text = va.toString;
    
}
-(void)btnClick2{
    
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext） 相当于js环境中的 window
    JSContext *context = [self.web valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    JSValue *va = [context evaluateScript:@"jsFunc()"];
    self.lab.text = va.toString;
}


-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext） 相当于js环境中的 window
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    

//    // 2.1 JSExport 协议方式, 实现
//    // 创建一个遵循 JSExport的 XWFT类, 挂载到js的全局window中
//    XWFT *xwft = [[XWFT alloc] init];
//    context[@"xwft"] = xwft;
//
//
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    
    
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    // 2.2 如果不想通过 JSExport 的方式进行处理
    // 可以如下操作, 通过传递 oc的Block,
//    [context evaluateScript:@"var xwft2 = {}"]; // 向js 注入一个 xwft 对象
    
//    context[@"xwft2"][@"getLocation"] = ^(){
//        NSArray *args =[JSContext currentArguments];
//        NSLog(@"%@",args);
//        JSValue *cb = [args[1] callWithArguments:@[@"sb"]];
//    };
//
}

- (void)viewDidAppear:(BOOL)animated{
    
    
    
}



@end
